package com.zzty.servlet;

import com.zzty.common.Log;
import com.zzty.common.ReadFile;
import com.zzty.common.ReadFileNames;
import com.zzty.entity.FileMessage;
import com.zzty.entity.Groups;
import com.zzty.entity.User;
import com.zzty.service.GroupService;
import com.zzty.service.TeamExamService;
import com.zzty.service.TeamExamTransactionService;
import com.zzty.serviceimpl.GroupServiceImpl;
import com.zzty.serviceimpl.TeamExamServiceImpl;
import com.zzty.serviceimpl.TeamExamTransactionServiceImpl;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet(name = "TeamExamTransactionServlet",urlPatterns = "/TeamExamTransactionServlet")
public class TeamExamTransactionServlet extends HttpServlet {
    private static final Logger logger = LoggerFactory.getLogger(TeamExamTransactionServlet.class);
    TeamExamTransactionService tets=new TeamExamTransactionServiceImpl();
    TeamExamService tes=new TeamExamServiceImpl();
    GroupService gs=new GroupServiceImpl();
    ReadFileNames readFileNames=new ReadFileNames();
    ReadFile readFile=new ReadFile();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("method");
        if("checkToday".equals(method)){
            checkToday(request,response);
        }
        if("setFileName".equals(method)){
            setFileName(request,response);
        }
        if("setBeginTime".equals(method)){
            setBeginTime(request,response);
        }
        if("getGroupMessage".equals(method)){
            getGroupMessage(request,response);
        }
        if("setScore".equals(method)){
            setScore(request,response);
        }
        if("checkTodaySubmit".equals(method)){
            checkTodaySubmit(request,response);
        }
        if("getBeginTime".equals(method)){
            getBeginTime(request,response);
        }
    }
    protected void checkTodaySubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user=(User)request.getSession().getAttribute("user");
        String userName=user.getName();
        Integer groupId=gs.getIdByUserName(userName);
        String date=request.getParameter("date");
        boolean result=false;
        result=tets.checkTodaySubmit(date,groupId);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(result);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }


    protected void checkToday(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user=(User)request.getSession().getAttribute("user");
        String userName=user.getName();
        Integer groupId=gs.getIdByUserName(userName);
        String date=request.getParameter("date");
        boolean result=false;
        result=tets.checkToday(date,groupId);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(result);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }
    protected void setScore(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer score=Integer.parseInt(request.getParameter("score"));
        String Dtime=request.getParameter("Dtime");
        Integer groupId=Integer.parseInt(request.getParameter("groupId"));
        boolean result=tets.setScore(Dtime,groupId,score);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(result);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }

    protected void setFileName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TeamExamTransactionService tets=new TeamExamTransactionServiceImpl();
        String Dtime=request.getParameter("Dtime");
        logger.info("Dtime:{}",Dtime);
        Integer groupId=Integer.parseInt(request.getParameter("groupId"));
        String url=tets.getUrl(Dtime,groupId);
        List<FileMessage> fileMessages=null;
        if(url!=null) {
            fileMessages=new ArrayList<FileMessage>();
            List<String> fileNames = readFileNames.readfile(url);
            for (String s : fileNames) {
                FileMessage fileMessage = new FileMessage();
                StringBuilder workContent = new StringBuilder("");
                workContent.append(readFile.readJava(url + "//" + s));
                fileMessage.setFileName(s);
                fileMessage.setWorkContent(workContent);
                fileMessages.add(fileMessage);
            }
        }


        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(fileMessages);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }
    protected void setBeginTime(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user=(User)request.getSession().getAttribute("user");
        String userName=user.getName();
        System.out.println("userName="+userName);
        Integer groupId=gs.getIdByUserName(userName);
        String startTime=request.getParameter("startTime");
        String date=request.getParameter("date");
        boolean result=false;
        result=tets.setBeginTimeAndUserId(startTime,date,groupId);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(result);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }
    protected void getGroupMessage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String date=request.getParameter("date");
        List<Groups> groups=null;
        groups=tets.getGroupsByDate(date);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(groups);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }
    protected void getBeginTime(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user=(User)request.getSession().getAttribute("user");
        String userName=user.getName();
        Integer groupId=gs.getIdByUserName(userName);
        String date=request.getParameter("date");
        String beginTime=tets.getBeginTime(date,groupId);
        System.out.println("beginTime="+beginTime);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(beginTime);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }
}
